#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/eventfd.h>
#include <pthread.h>
int evfd = -1;
void *wait(void *args)
{
    std::string name = (const char *)args;
    while (true)
    {
        uint64_t flag = 0;
        ssize_t n = read(evfd, &flag, sizeof(flag));
        std::cout << name << "被唤醒!" << "flag is a " << flag << std::endl;
    }
    return nullptr;
}
void *wakeup(void *args)
{
    std::string name = (const char *)args;
    while (true)
    {
        uint64_t flag = 3;
        ssize_t n = ::write(evfd, &flag, sizeof(flag));
        std::cout<<name<<"写入数据!"<<std::endl;
        sleep(1);
    }
    return nullptr;
}
int main()
{
    evfd = ::eventfd(0, 0);
    pthread_t tid1, tid2;
    pthread_create(&tid1, nullptr, wait, (void *)"thread-1");
    pthread_create(&tid2, nullptr, wakeup, (void *)"thread-2");
    pthread_join(tid1,nullptr);
    pthread_join(tid2,nullptr);
    return 0;
}